package org.lixf.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {

    private static final String URL;
    private static final String USER;
    private static final String PASSWORD;

    static {
        URL = "jdbc:mysql://localhost:3306/oa?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8";
        PASSWORD = "root";
        USER = "root";
    }

    public static void test01() {
        try {
            Driver driver = new com.mysql.cj.jdbc.Driver();
            Properties info = new Properties();
            info.setProperty("user", USER);
            info.setProperty("password", PASSWORD);
            Connection conn = driver.connect(URL, info);
            System.out.println(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void test02() {
        try {
            Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver");
            Driver driver = (Driver) clazz.newInstance();
            Properties info = new Properties();
            info.setProperty("user", USER);
            info.setProperty("password", PASSWORD);
            Connection conn = driver.connect(URL, info);
            System.out.println(conn);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
            e.printStackTrace();
        }
    }

    public static void test03() {
        try {
            Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver");
            Driver driver = (Driver) clazz.newInstance();
            DriverManager.registerDriver(driver);
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            System.out.println(conn);
        } catch (SQLException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void test04() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // Driver driver = new com.mysql.cj.jdbc.Driver();
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            System.out.println(conn);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 最终版本
    public static void test05() {
        try {
            InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties info = new Properties();
            info.load(is);
            String url = info.getProperty("url");
            String user = info.getProperty("user");
            String password = info.getProperty("password");
            String driver = info.getProperty("driver");

            Class.forName(driver); // 可以不再需要Class.forName --> META-INF/services/java.sql.Driver
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
        } catch (SQLException | ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // test01();
        // test02();
        // test03();
        // test04();
        test05();

    }
}
